package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.api.db.QuestionApi;
import com.tanhua.dubbo.api.mapper.QuestionMapper;
import com.tanhua.model.db.Question;
import com.tanhua.model.db.Settings;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 问题服务接口
 */
@DubboService
public class QuestionApiImpl implements QuestionApi {

    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 1根据当前用户id调用服务查询陌生人问题
     */
    @Override
    public Question findByUserId(Long userId) {
        QueryWrapper<Question> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        return questionMapper.selectOne(queryWrapper);
    }

    /**
     * 设置陌生人问题 - 保存
     * @param question
     */
    @Override
    public void saveQuestions(Question question) {
        //1.根据当前用户id问题表记录是否存在
        Question qt = findByUserId(question.getUserId());
        //2 如果不存在 则保存问题
        if(qt == null){
            questionMapper.insert(question);
        }else{
            //3 如果存在，则更新问题
            qt.setTxt(question.getTxt());//将用户修改的问题 设置到qt对象中
            questionMapper.updateById(qt);
        }
    }
}
